// pages/health/settings.js
const app = getApp();
const { storage } = require('../../utils/storage.js');

Page({
  data: {
    // 生理期设置
    cycleLength: 28,      // 周期长度（天）
    periodLength: 5,      // 经期长度（天）
    
    // 智能预测设置
    enablePrediction: true,  // 开启智能预测
    
    // 通知设置
    enableNotification: true,  // 开启应用通知
    notificationTime: '09:00', // 通知时间
    
    // 颜色设置
    periodColor: '#FF6B6B',        // 经期颜色
    predictedPeriodColor: '#FFB6C1', // 预测经期颜色
    ovulationDayColor: '#9370DB',   // 排卵日颜色
    fertilePeriodColor: '#87CEFA',  // 排卵期颜色
    
    // 通知时间选择范围
    timeRange: []
  },

  onLoad() {
    this.initTimeRange();
    this.loadSettings();
  },

  // 初始化时间范围
  initTimeRange() {
    const timeRange = [];
    for (let hour = 0; hour < 24; hour++) {
      for (let minute = 0; minute < 60; minute += 30) {
        const time = `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`;
        timeRange.push(time);
      }
    }
    this.setData({ timeRange });
  },

  // 加载设置
  loadSettings() {
    const settings = storage.get('healthSettings') || {};
    
    this.setData({
      cycleLength: settings.cycleLength || 28,
      periodLength: settings.periodLength || 5,
      enablePrediction: settings.enablePrediction !== false, // 默认开启
      enableNotification: settings.enableNotification !== false, // 默认开启
      notificationTime: settings.notificationTime || '09:00',
      periodColor: settings.periodColor || '#FF6B6B',
      predictedPeriodColor: settings.predictedPeriodColor || '#FFB6C1',
      ovulationDayColor: settings.ovulationDayColor || '#9370DB',
      fertilePeriodColor: settings.fertilePeriodColor || '#87CEFA'
    });
  },

  // 保存设置
  saveSettings() {
    const settings = {
      cycleLength: this.data.cycleLength,
      periodLength: this.data.periodLength,
      enablePrediction: this.data.enablePrediction,
      enableNotification: this.data.enableNotification,
      notificationTime: this.data.notificationTime,
      periodColor: this.data.periodColor,
      predictedPeriodColor: this.data.predictedPeriodColor,
      ovulationDayColor: this.data.ovulationDayColor,
      fertilePeriodColor: this.data.fertilePeriodColor
    };
    
    storage.set('healthSettings', settings);
    
    // 更新全局设置
    app.globalData.healthSettings = settings;
    
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    });
    
    // 返回上一页
    setTimeout(() => {
      wx.navigateBack();
    }, 1000);
  },

  // 周期长度变化
  onCycleLengthChange(e) {
    this.setData({
      cycleLength: parseInt(e.detail.value)
    });
  },

  // 经期长度变化
  onPeriodLengthChange(e) {
    this.setData({
      periodLength: parseInt(e.detail.value)
    });
  },

  // 智能预测开关
  onPredictionSwitchChange(e) {
    this.setData({
      enablePrediction: e.detail.value
    });
  },

  // 通知开关
  onNotificationSwitchChange(e) {
    this.setData({
      enableNotification: e.detail.value
    });
  },

  // 通知时间变化
  onNotificationTimeChange(e) {
    this.setData({
      notificationTime: this.data.timeRange[e.detail.value]
    });
  },

  // 经期颜色变化
  onPeriodColorChange(e) {
    this.setData({
      periodColor: e.detail.value
    });
  },

  // 预测经期颜色变化
  onPredictedPeriodColorChange(e) {
    this.setData({
      predictedPeriodColor: e.detail.value
    });
  },

  // 排卵日颜色变化
  onOvulationDayColorChange(e) {
    this.setData({
      ovulationDayColor: e.detail.value
    });
  },

  // 排卵期颜色变化
  onFertilePeriodColorChange(e) {
    this.setData({
      fertilePeriodColor: e.detail.value
    });
  }
});